package com.ch.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ch.web.ResponseDto.xyComment.XyCommentListResponse;
import com.ch.web.entity.user.XyCommentEntity;
import com.ch.web.mapper.XyCommentDao;
import com.ch.web.redis.UserMapCache;
import com.ch.web.service.XyBackCommentService;
import com.ch.web.service.XyCommentService;
import com.ch.web.service.XyTalkService;
import com.ch.web.service.XyUserService;
import com.ch.web.utils.PageUtils;
import com.ch.web.utils.Query;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("xyCommentService")
public class XyCommentServiceImpl extends ServiceImpl<XyCommentDao, XyCommentEntity> implements XyCommentService {

    @Autowired
    UserMapCache userMapCache;
    @Autowired
    XyBackCommentService xyBackCommentService;
    @Resource
    XyUserService xyUserService;
    @Resource
    XyTalkService xyTalkService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<XyCommentEntity> queryWrapper = new QueryWrapper<>();
        String commentUser = (String) params.get("commentUser");
        if (StringUtils.isNotBlank(commentUser)){
            Long id = xyUserService.geUserIdByUserNick(commentUser);
            if (ObjectUtils.isEmpty(id)){
                return new PageUtils(new Page<>());
            }
            queryWrapper.eq("user_id",id);
        }
        String commentInfo = (String) params.get("commentInfo");
        if (StringUtils.isNotBlank(commentInfo)){
            queryWrapper.like("comment",commentInfo);
        }
        IPage<XyCommentEntity> page = this.page(
                new Query<XyCommentEntity>().getPage(params),
                queryWrapper
        );
        page.setRecords(page.getRecords().stream().map(xyCommentEntity -> {
            XyCommentListResponse resp = new XyCommentListResponse();
            BeanUtils.copyProperties(xyCommentEntity,resp);
            resp.setCommentUser(xyUserService.getById(xyCommentEntity.getUserId()).getUserNick());
            resp.setCommentTargetInfo(xyTalkService.getById(xyCommentEntity.getTalkId()).getContent());
            return resp;
        }).collect(Collectors.toList()));
        return new PageUtils(page);
    }

    @Override
    public List<XyCommentEntity> getCommentByTalkId(Long talkId) {
        QueryWrapper<XyCommentEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("talk_id", talkId);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public Long saveComment(XyCommentEntity xyCommentEntity, HttpServletRequest request) {
        Long userIdByToken = userMapCache.getUserIdByToken(request);
        xyCommentEntity.setUserId(userIdByToken);
        return this.save(xyCommentEntity) ? xyCommentEntity.getId() : null;
    }

    @Override
    public boolean deleteCommentByTalkId(Long id) {
        QueryWrapper<XyCommentEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("talk_id", id);
        List<XyCommentEntity> list = this.list(wrapper);
        if (list.size() > 0) {
            list.forEach(xyCommentEntity -> {
                this.xyBackCommentService.deleteBackCommentByCommentId(xyCommentEntity.getId());
            });
            return this.remove(wrapper);
        } else {
            return true;
        }
    }
}
